﻿using Admin.ApplicationLib;
using Admin.ApplicationLib.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Admin.WebApi.Infrastructure.Store
{
    public class CustomRoleStore : RoleStore<RoleEntity, AdminApplicationDbContext, Guid>
    {
        private readonly AdminApplicationDbContext _context;

        public CustomRoleStore(AdminApplicationDbContext context, IdentityErrorDescriber? describer = null) : base(context,
            describer)
        {
            _context = context;
        }

        public override async Task<RoleEntity> FindByNameAsync(string normalizedName, CancellationToken cancellationToken = new CancellationToken())
        {
            return (await (from role in _context.Roles
                where role.Code == normalizedName
                select role).FirstOrDefaultAsync(cancellationToken:cancellationToken))!;
        }
    }
}